package com.dream.rpc.server;


import io.vertx.core.Vertx;

import java.rmi.ServerError;

/**
 * @author Dreamer
 */
public class VertxHttpServer implements HttpServer{
    @Override
    public void doStart(int port) {

        //创建vertx实例
        Vertx vertx = Vertx.vertx();

        //创建http服务器
        io.vertx.core.http.HttpServer httpServer = vertx.createHttpServer();

        //监听接口并处理请求（每当有请求到达都会执行，请求的响应）
//        httpServer.requestHandler(request -> {
//            System.out.println("Received request:" + request.method() + " " + request.uri());
//
//            //发送http响应
//            request.response()
//                    .putHeader("content-type", "text/plain")
//                    .end("Hello from Vert.x Http server");
//        });
        httpServer.requestHandler(new HttpServerHandler());

        //将服务注册到8080端口并监听
        httpServer.listen(port,result->{
            if (result.succeeded()) {
                System.out.println("Server is now listening on port" + port);
            } else {
                System.out.println("Failed to start server:" + result.cause());
            }
        });
    }
}
